package com.leyou.item.service;

import com.leyou.common.exception.pojo.ExceptionEnum;
import com.leyou.common.exception.pojo.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class SpecService {

    @Autowired
    private SpecGroupMapper groupMapper;

    @Autowired
    private SpecParamMapper paramMapper;


    public List<SpecGroup> findSpecGroupByCid(Long id) {
        SpecGroup record = new SpecGroup();
        record.setCid(id);
        List<SpecGroup> specGroups = groupMapper.select(record);
        if(CollectionUtils.isEmpty(specGroups)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return specGroups;
    }

    public List<SpecParam> findSpecParams(Long gid, Long cid, Boolean searching) {
        //将来的参数，可能是只传gig   或者   传cid和searching，由此得出结论，gid和cid必须有一个有值
        if(gid==null && cid==null){
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
        SpecParam record = new SpecParam();
        record.setGroupId(gid);
        record.setCid(cid);
        record.setSearching(searching);
        List<SpecParam> specParams = paramMapper.select(record);
        if(CollectionUtils.isEmpty(specParams)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return specParams;
    }

    public List<SpecGroupDTO> findSpecGroupDTOByCid(Long id) {
        //根据分类id查询规格组集合
        List<SpecGroup> specGroups = findSpecGroupByCid(id);
        //将规格组的集合转成规格组DTO集合
        List<SpecGroupDTO> specGroupDTOS = BeanHelper.copyWithCollection(specGroups, SpecGroupDTO.class);
        //一次将所有当前分类下所有规格参数查询出来【将之前分批查询规格参数改为一次查】
        List<SpecParam> specParams = findSpecParams(null, id, null);
        //采用流的方式将规格参数集合进行分组
        Map<Long, List<SpecParam>> specParamsMap = specParams.stream().collect(Collectors.groupingBy(SpecParam::getGroupId));
        //循环specGroupDTOS规格组集合
        specGroupDTOS.forEach(specGroupDTO -> {
            specGroupDTO.setParams(specParamsMap.get(specGroupDTO.getId()));
        });

        //嵌套循环将规格参数对象放入对应的规格组对象中
//        specGroupDTOS.forEach(specGroupDTO -> {
//            specParams.forEach(specParam -> {
//                if(specGroupDTO.getId().equals(specParam.getGroupId())){
//                    specGroupDTO.getParams().add(specParam);
//                }
//            });
//        });

        //遍历【java代码中要尽可能避免在循环里面执行数据库操作】
//        specGroupDTOS.forEach(specGroupDTO -> {
//            //根据规格组id查询规格参数集合
//            List<SpecParam> specParams = findSpecParams(specGroupDTO.getId(), null, null);
//            specGroupDTO.setParams(specParams);
//        });
        return specGroupDTOS;
    }
}
